package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.dto.LeadershipSaveDeviceDto;
import com.example.medical.entity.dto.LeadershipUpdateDeviceDto;
import com.example.medical.entity.dto.UserGetDeviceListDto;
import com.example.medical.entity.pojo.*;
import com.example.medical.mapper.*;
import com.example.medical.service.DeviceService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
* @author 24354
* @description 针对表【device】的数据库操作Service实现
* @createDate 2024-03-31 22:42:11
*/
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device>
    implements DeviceService{

    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public Result UserGetDeviceList(UserGetDeviceListDto dto) {

        dto.checkParam();
        IPage<Device> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Device> wrapper=new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(Device::getDeviceName,dto.getName());
        }
        if (dto.getDeviceTypeId()!=null) {
            wrapper.eq(Device::getDeviceTypeId,dto.getDeviceTypeId());
        }
        if (dto.getDeviceDivisionId()!=null) {
            wrapper.eq(Device::getDeviceDivisionId,dto.getDeviceDivisionId());
        }
        if (dto.getDeviceStoreroomId()!=null) {
            wrapper.eq(Device::getDeviceStoreroomId,dto.getDeviceStoreroomId());
        }
        if (dto.getDeviceStoreroomTypeId()!=null) {
            wrapper.eq(Device::getDeviceStoreroomTypeId,dto.getDeviceStoreroomTypeId());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());

        return result;
    }

    @Autowired
    private LeadershipMapper leadershipMapper;
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;
    @Autowired
    private StoreroomMapper storeroomMapper;
    @Autowired
    private StoreroomTypeMapper storeroomTypeMapper;
    @Autowired
    private DivisionMapper divisionMapper;
    @Override
    public Result LeadershipSaveDevice(LeadershipSaveDeviceDto dto) {
        Long leadershipId = BaseContext.getCurrentId();
        if (leadershipId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        DeviceType checkDeviceType = deviceTypeMapper.selectById(dto.getTypeId());
        Storeroom checkStoreroom = storeroomMapper.selectById(dto.getStoreroomId());
        Division checkDivision = divisionMapper.selectById(dto.getDivisionId());
        if (checkDeviceType==null||checkStoreroom==null||checkDivision==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Device device=new Device();
        device= device.builder()
                .deviceNumber(dto.getNumber())
                .deviceName(dto.getName())
                .deviceTypeId(checkDeviceType.getDeviceTypeId())
                .deviceTypeName(checkDeviceType.getDeviceTypeName())
                .deviceModel(dto.getModel())
                .deviceInventory(dto.getQuantity())
                .deviceStoreroomId(checkStoreroom.getStoreroomId())
                .deviceStoreroomName(checkStoreroom.getStoreroomName())
                .deviceStoreroomNumber(checkStoreroom.getStoreroomNumber())
                .deviceStoreroomAddress(checkStoreroom.getStoreroomAddress())
                .deviceStoreroomTypeId(checkStoreroom.getStoreroomTypeId())
                .deviceStoreroomTypeName(checkStoreroom.getStoreroomTypeName())
                .deviceDivisionId(checkDivision.getDivisionId())
                .deviceDivisionName(checkDivision.getDivisionName())
                .deviceMessage(dto.getMessage())
                .saveTime(String.valueOf(LocalDateTime.now()))
                .build();
        boolean checkSave = save(device);
        if (!checkSave) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result LeadershipUpdateDevice(LeadershipUpdateDeviceDto dto) {
        Long leadershipId = BaseContext.getCurrentId();
        if (leadershipId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Device checkDevice = deviceMapper.selectById(dto.getId());
        if (checkDevice==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Device device=new Device();
        BeanUtils.copyProperties(checkDevice,device);
        if (StringUtils.isNotBlank(dto.getNumber())) {
            device.setDeviceNumber(dto.getNumber());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            device.setDeviceName(dto.getName());
        }
        if (dto.getTypeId()!=null) {
            DeviceType checkDeviceType = deviceTypeMapper.selectById(dto.getTypeId());
            if (checkDeviceType==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            device.setDeviceTypeId(checkDeviceType.getDeviceTypeId());
            device.setDeviceTypeName(checkDeviceType.getDeviceTypeName());
        }
        if (StringUtils.isNotBlank(dto.getModel())) {
            device.setDeviceModel(dto.getModel());
        }
        if (dto.getQuantity()!=null) {
            device.setDeviceInventory(dto.getQuantity());
        }
        if (dto.getStoreroomId()!=null) {
            Storeroom checkStoreroom = storeroomMapper.selectById(dto.getStoreroomId());
            if (checkStoreroom==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            device.setDeviceStoreroomId(checkStoreroom.getStoreroomId());
            device.setDeviceStoreroomName(checkStoreroom.getStoreroomTypeName());
            device.setDeviceStoreroomNumber(checkStoreroom.getStoreroomNumber());
            device.setDeviceStoreroomTypeId(checkStoreroom.getStoreroomTypeId());
            device.setDeviceStoreroomAddress(checkStoreroom.getStoreroomAddress());
            device.setDeviceStoreroomTypeName(checkStoreroom.getStoreroomTypeName());
        }
        if (dto.getDivisionId()!=null) {
            Division checkDivision = divisionMapper.selectById(dto.getDivisionId());
            if (checkDivision==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            device.setDeviceDivisionId(checkDivision.getDivisionId());
            device.setDeviceDivisionName(checkDivision.getDivisionName());
        }
        if (StringUtils.isNotBlank(dto.getMessage())) {
            device.setDeviceMessage(dto.getMessage());
        }
        boolean checkUpdate = updateById(device);
        if (!checkUpdate) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result LeadershipRemoveDeviceArr(String[] ids) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        for (String id:ids) {
            Device checkDevice = deviceMapper.selectById(id);
            if (checkDevice==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            boolean checkRemove = removeById(id);
            if (!checkRemove) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public boolean UpdateDeviceNumber(Affairs checkAffairs,Integer a) {
        //数量修改
        Device checkDevice = deviceMapper.selectById(checkAffairs.getAffairsDeviceId());
        if (checkAffairs==null) {
            return false;
        }
        BigDecimal old=new BigDecimal(checkDevice.getDeviceNumber());
        BigDecimal aff=new BigDecimal(checkAffairs.getAffairsNumber());
        String number = "";
        if (a==0) {
            number = old.subtract(aff).toString();
        }
        if (a==1) {
            number = old.add(aff).toString();
        }
        Device device=new Device();
        device= device.builder()
                .deviceId(checkDevice.getDeviceId())
                .deviceNumber(number)
                .build();
        int checkUpdateDevice = deviceMapper.updateById(device);
        if (checkUpdateDevice==0) {
            return false;
        }
        return true;
    }

}




